package com.ideal.gui;

import com.ideal.constant.LocalPathConstant;
import com.ideal.request.ExportDocRequest;
import com.ideal.response.BaseResponse;
import com.ideal.service.ExportDocService;
import com.ideal.service.impl.ExportDocServiceImpl;
import com.ideal.util.SystemUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.io.File;

/**
 * 导出文档图形界面
 *
 * @author baiwenhuang
 * @date 2023-08-05 02:37
 **/
public class ExportGui extends JFrame {
    JFrame frame;
    JButton selectButton, exportButton;
    JTextField apiUrlField, tokenField, repoNamespaceField, exportLocalDirField;
    JPanel panel;
    JLabel apiUrlLabel, tokenLabel, repoNamespaceLabel, exportLocalDirLabel;
    File exportDir;
    JTextArea resultLogArea;

    public static void main(String[] args) {
        new ExportGui();
    }

    public ExportGui() {
        // 这是一个窗口类
        frame = new JFrame();
        // 设置窗口位置与大小
        frame.setBounds(100, 100, 680, 570);
        // 用户关闭时会关闭所有窗口
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置这窗口可见,记得尽量要把这一句放在最后面
        frame.setVisible(Boolean.TRUE);

        // 创建一个面板,并且将这个面板添加到窗口里面去
        panel = new JPanel();
        panel.setLayout(null);
        frame.add(panel);

        apiUrlLabel = new JLabel("语雀 API 地址");
        apiUrlLabel.setBounds(30, 50, 100, 35);
        panel.add(apiUrlLabel);

        apiUrlField = new JTextField();
        apiUrlField.setBounds(130, 50, 400, 35);
        apiUrlField.setEditable(true);
        panel.add(apiUrlField);
        apiUrlField.setText("https://www.yuque.com/api/v2");
        apiUrlField.setEnabled(false);

        tokenLabel = new JLabel("语雀 Token");
        tokenLabel.setBounds(30, 100, 100, 35);
        panel.add(tokenLabel);

        tokenField = new JTextField();
        tokenField.setBounds(130, 100, 400, 35);
        tokenField.setEditable(true);
        panel.add(tokenField);

        repoNamespaceLabel = new JLabel("知识库命名空间");
        repoNamespaceLabel.setBounds(30, 150, 100, 35);
        panel.add(repoNamespaceLabel);

        repoNamespaceField = new JTextField();
        repoNamespaceField.setBounds(130, 150, 400, 35);
        repoNamespaceField.setEditable(true);
        panel.add(repoNamespaceField);

        repoNamespaceField.setText("ideal-20/java-ideal-interview");

        exportLocalDirLabel = new JLabel("导出目录");
        exportLocalDirLabel.setBounds(30, 200, 100, 35);
        panel.add(exportLocalDirLabel);

        exportLocalDirField = new JTextField();
        exportLocalDirField.setBounds(130, 200, 400, 35);
        exportLocalDirField.setEditable(false);
        panel.add(exportLocalDirField);

        if (SystemUtils.isWindows()) {
            exportLocalDirField.setText(System.getProperty("user.home") + "\\Desktop\\语雀导出");
        } else if(SystemUtils.isMacOs()){
            exportLocalDirField.setText(System.getProperty("user.home") + "/Desktop/语雀导出");
        }

        resultLogArea = new JTextArea();
        resultLogArea.setBounds(30, 250, 500, 260);
        panel.add(resultLogArea);

        selectButton = new JButton("选择");
        selectButton.setBounds(550, 200, 100, 35);
        selectButton.setVisible(Boolean.TRUE);
        panel.add(selectButton);

        exportButton = new JButton("执行");
        exportButton.setBounds(550, 300, 100, 35);
        exportButton.setVisible(Boolean.TRUE);
        panel.add(exportButton);

        // 文件选择按钮监听
        selectButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.showDialog(new JLabel(), "选择");
            exportDir = fileChooser.getSelectedFile();
            exportLocalDirField.setText(exportDir.getAbsolutePath());
        });

        // 执行按钮监听
        exportButton.addActionListener(e -> {
            exportButton.setEnabled(Boolean.FALSE);
            String apiUrl = apiUrlField.getText();
            String token = tokenField.getText();
            String repoNamespace = repoNamespaceField.getText();
            String exportLocalDirPath = "";
            if (exportDir != null) {
                exportLocalDirPath = exportDir.getAbsolutePath();
            } else {
                exportLocalDirPath = exportLocalDirField.getText();
            }

            if (StringUtils.isAnyEmpty(apiUrl, token, repoNamespace, exportLocalDirPath)) {
                JOptionPane.showMessageDialog(null, "请输入完整信息", "提示", JOptionPane.WARNING_MESSAGE);
                exportButton.setEnabled(Boolean.TRUE);
                return;
            }
            ExportDocService exportDocService = new ExportDocServiceImpl();
            ExportDocRequest exportDocRequest = new ExportDocRequest();
            exportDocRequest.setApiUrl(apiUrl);
            exportDocRequest.setToken(token);
            exportDocRequest.setRepoNamespace(repoNamespace);
            exportDocRequest.setExportLocalDirPath(exportLocalDirPath);
            BaseResponse<String> response = exportDocService.exportAllDocOfRepo(exportDocRequest);
            if (response.isSuccess()) {
                String resultLog = response.getData();
                if (StringUtils.isNotBlank(resultLog)) {
                    resultLogArea.setText(resultLog);
                }
                JOptionPane.showMessageDialog(null, "导出成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, response.getMessage(), "提示", JOptionPane.WARNING_MESSAGE);
            }
            exportButton.setEnabled(Boolean.TRUE);
        });

        // 设置这窗口可见,记得尽量要把这一句放在最后面
        frame.setVisible(true);
    }
}